import fs from 'node:fs'

if (!global.segment) {
  global.segment = (await import("oicq")).segment
}

if (!global.uploadRecord) {
  try {
    global.uploadRecord = (await import("./model/uploadRecord.js")).default
  } catch (err) {
    global.uploadRecord = segment.record
  }
}

const files = fs.readdirSync('./plugins/dsplugin-plugin/apps').filter(file => file.endsWith('.js'))

let ret = []
logger.info("--------------------------------")
logger.info("dsplugin-plugin载入成功");
logger.info("开发者 3446737520");
logger.info("我们的群聊：600388843")
logger.info("更多功能正在开发中")
logger.info("--------------------------------")


files.forEach((file) => {
    ret.push(import(`./apps/${file}`))
})

ret = await Promise.allSettled(ret)

let apps = {}
for (let i in files) {
    let name = files[i].replace('.js', '')

    if (ret[i].status != 'fulfilled') {
        logger.error(`载入插件错误：${logger.red(name)}`)
        logger.error(ret[i].reason)
        continue
    }
    apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]
}
export { apps }